package so.ruo.api;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpTools {

	private CloseableHttpResponse response = null;
	private HttpClientContext context = null;
	private CloseableHttpClient httpclient = null;
	private HttpHost targetHost = null;
	private HttpHost proxyHost = null;
	private CookieStore cookieStore = null;
	private Proxy proxy = null;
	private String charset = "GBK";

	/**
	 * 正常GET方式HTTP请求
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String getRequest(String url) throws ClientProtocolException, IOException {
		HttpGet get = new HttpGet(url);
		if (proxy != null) {
			System.out.println("target:"+targetHost.getHostName()+"======");
			response = httpclient.execute(targetHost, get, context);
		} else {
			response = httpclient.execute(get, context);
		}
		//System.out.println(response.getStatusLine());
		HttpEntity entity = response.getEntity();
		String content = EntityUtils.toString(entity, charset);
		// System.out.println(content);
		/* EntityUtils.consume(entity); */
		return content;
	}

	/**
	 * 正常POST方式HTTP请求
	 * 
	 * @param url
	 * @param parms
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String postRequest(String url, List<NameValuePair> parms) throws ClientProtocolException, IOException {
		HttpPost post = new HttpPost(url);
		post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
		post.addHeader("Content-Type", "application/x-www-form-urlencoded");
		// post.addHeader(&quot;Referer&quot;, &quot;http://2013.weibo.com/&quot;);
		UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(parms, "UTF-8");
		post.setEntity(postEntity);
		if (proxy != null) {
			response = httpclient.execute(targetHost, post, context);
		} else {
			response = httpclient.execute(post, context);
		}
		HttpEntity entity = response.getEntity();
		String content = EntityUtils.toString(entity, charset);
		/* EntityUtils.consume(entity); */
		return content;
	}

	public CloseableHttpResponse getResponse() {
		return response;
	}

	public void setResponse(CloseableHttpResponse response) {
		this.response = response;
	}

	public HttpClientContext getContext() {
		return context;
	}

	public void setContext(HttpClientContext context) {
		this.context = context;
	}

	public CloseableHttpClient getHttpclient() {
		return httpclient;
	}

	public void setHttpclient(CloseableHttpClient httpclient) {
		this.httpclient = httpclient;
	}

	public HttpHost getTargetHost() {
		return targetHost;
	}

	public void setTargetHost(HttpHost targetHost) {
		this.targetHost = targetHost;
	}

	public HttpHost getProxyHost() {
		return proxyHost;
	}

	public void setProxyHost(HttpHost proxyHost) {
		this.proxyHost = proxyHost;
	}

	public CookieStore getCookieStore() {
		return cookieStore;
	}

	public void setCookieStore(CookieStore cookieStore) {
		this.cookieStore = cookieStore;
	}

	public Proxy getProxy() {
		return proxy;
	}

	public void setProxy(Proxy proxy) {
		this.proxy = proxy;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

}